﻿using System;
using System.Collections.Generic;
using System.Text;
using FieldDevice.ModBus;
using FieldDevice.Emulation;
using FieldLib;
using FieldDevice;
namespace FieldDevice.Emulation
{
    class EM400Emulator : DeviceEmulator
    {
        public EM400Emulator(ushort address)
            :base(address)
        {
            this.timeEM400Emulation = new System.Windows.Forms.Timer();
            this.timeEM400Emulation.Enabled = true;
            this.timeEM400Emulation.Interval = 1000;
            this.timeEM400Emulation.Tick += new EventHandler(this.timeEM400Emulation_tick);
            DataEmulator = new DataEmulatorForm(address);
            DataEmulator.Show();
        }


        DataEmulatorForm DataEmulator;
        private System.Windows.Forms.Timer timeEM400Emulation;

        public const ushort ADDR_YX = 2000;
        public  const ushort ADDR_IA =2002;
        public  const ushort ADDR_IB = 2003;
        public  const ushort ADDR_IC = 2004;
        public const ushort ADDR_Vab = 2006;
        public const ushort ADDR_Vbc = 2007;
        public const ushort ADDR_Vca = 2008;
        public  const ushort ADDR_VA = 2009;
        public  const ushort ADDR_VB = 2010;
        public  const ushort ADDR_VC = 2011;
       
        public const ushort ADDR_FR = 2012;
        public const ushort ADDR_W = 2013;
        public const ushort ADDR_Q = 2014;
        public const ushort ADDR_S = 2015;
        public const ushort ADDR_PF = 2016;
        public const ushort ADDR_EP_Hi = 2017;
        public const ushort ADDR_EP_Low = 2018;
        public const ushort ADDR_EQ_Hi = 2019;
        public const ushort ADDR_EQ_Low = 2020;

        public ushort  IA;      //寄存A相电流
        public ushort IB;       //寄存B相电流
        public ushort IC;       //寄存C相电流

        public ushort UA;       //寄存A相电压
        public ushort UB;       //寄存B相电压
        public ushort UC;       //寄存C相电压

        byte[] ModBuffer = new byte[1 + 2 + 2 + 256 * 2 + 2 + 2 + 1];
        int DataLen=0;





        private void timeEM400Emulation_tick(object sender, EventArgs e)
        {
            Random randomNum = new Random();
            this.IA = (ushort)randomNum.Next(50);
            DataEmulator.IA = Convert.ToString(this.IA);
            this.IB = (ushort)randomNum.Next(50);
            DataEmulator.IB = Convert.ToString(this.IB);
            this.IC = (ushort)randomNum.Next(50);
            DataEmulator.IC = Convert.ToString(this.IC);

            this.UA = (ushort)randomNum.Next(350);
            DataEmulator.VA = Convert.ToString(this.UA);
            this.UB = (ushort)randomNum.Next(350);
            DataEmulator.VB = Convert.ToString(this.UB);
            this.UC = (ushort)randomNum.Next(350);
            DataEmulator.VC = Convert.ToString(this.UC);
        }

             public override byte[] ProcessModBusPacket(ModBusPacket packet)
        {
            ModBusPacket tmpPacket = packet;
            tmpPacket = CRCVerify(tmpPacket);
            if (packet.CRCH != tmpPacket.CRCH || packet.CRCL != tmpPacket.CRCL)
            {
                packet.DataNumberHi = 0xFF;
                packet.DataNumberLow = 0xFF;
            }

            switch (packet.Function)
            {
                case ModBus.ModBusFunction.READINPUTREGISTER:
                    ReadRegister(packet);
                    break;
                default:
                    break;
            }
            packet = CRCVerify(packet);
            ModBuffer[2 * DataLen + 3] = packet.CRCH;
            ModBuffer[2 * DataLen + 4] = packet.CRCL;
            return ModBuffer;
                
        }

        ModBusPacket ReadRegister(ModBusPacket input)
        {
            ModBusPacket response = new ModBusPacket();
            ushort tmpAddressData;
            ushort tmpData;
            response.SlaveAddress = input.SlaveAddress;
            response.Function = input.Function;
            tmpAddressData = input.DataAddressHi;
            tmpAddressData <<= 8;
            tmpAddressData += input.DataAddressLow;

            switch (tmpAddressData)
            {
                case ADDR_IA:
                    tmpData = IA;
                    response.DataNumberLow = (byte)(tmpData & 0x00ff);
                    tmpData = IA;
                    response.DataNumberHi = (byte)(tmpData >>= 8);
                    break;
                case ADDR_IB:
                    tmpData = IB;
                    response.DataNumberLow = (byte)(tmpData & 0x00ff);
                    tmpData = IB;
                    response.DataNumberHi = (byte)(tmpData >>= 8);
                    break;
                case ADDR_IC:
                    tmpData = IC;
                    response.DataNumberLow = (byte)(tmpData & 0x00ff);
                    tmpData = IC;
                    response.DataNumberHi = (byte)(tmpData >>= 8);
                    break;
                case ADDR_VB:
                    tmpData = UB;
                    response.DataNumberLow = (byte)(tmpData & 0x00ff);
                    tmpData = UB;
                    response.DataNumberHi = (byte)(tmpData >>= 8);
                    break;
                case ADDR_VC:
                    tmpData = UC;
                    response.DataNumberLow = (byte)(tmpData & 0x00ff);
                    tmpData = UC;
                    response.DataNumberHi = (byte)(tmpData >>= 8);
                    break;
            
            }

            return response;
        }
    }

    
     
}
